/**
 * 试题异步(分批加载试题)
 */

function question_score_ajax(){
    var _this = this;
    _this.body_id;
    _this.exam_no;
    _this.question_paper = 0;
    _this.questionIds;
    _this.load_length;

    _this.initMethod = function(body_id, exam_no){
        _this.load_length = $("#load_length").val();
        _this.body_id = body_id;
        _this.exam_no = exam_no;
        _this.initNumber();
        $("#loadQuestion").click(function () {
            _this.addQuestion();
            showAnswer();
        });
    };

    // 初始化每批加载的试题数量
    _this.initNumber = function () {
        var number_url = appPath + "/popup/question/exam_score_ajax/number";
        var params = {'exam_no': _this.exam_no};
        var data = _this.ajax(number_url, params);
        var code = data['code'];
        if(code == '10000'){
            _this.questionIds = data['result']['questions'];
            _this.questionLength = data['result']['length'];
            $("#total_length").val(_this.questionLength);
            if(_this.questionIds.length < 1){
                $("#loadQuestion").hide(_this.questionLength);
                //没题
                $("#" + _this.body_id).find('[id^=div]').end().append('<div><div class="no-content"></div></div>');
                return
            }
            _this.addQuestion();
        }
    };

    _this.initCard = function (len) {
        var aLi = '';
        var div_num = $('#card_list li').length;
        for( var i = 0; i < len; i++ ){
            // aLi += '<li>' + (i + 1 + _this.question_paper * _this.load_length) + '</li>';
            aLi += '<li>' + (i + 1 + div_num) + '</li>';
        }
        //加载答题卡
        $('#card_list').append(aLi);
        //答题卡部分模拟滚动条
        $('#card_list').niceScroll();
    };

    // 加载试题
    _this.addQuestion = function () {
        var url = appPath + "/popup/question/exam_score_ajax_step";
        var param = {"questionIds": _this.questionIds[_this.question_paper], "exam_no" : _this.exam_no};
        var data = _this.ajax(url, param);
        var code = data['code'];
        if(code == '10000'){
            var questions = data['result'];
            var html = '';
            if(questions.length < 1 && _this.question_paper == 0){
                html = '<div><div class="no-content"></div></div>';
            }
            var question_div_num = $("#" + _this.body_id).find('[id^=div]').length;
            var card_count = 0;
            for(var i=0; i<questions.length; i++){
               var question = questions[i];
                var chrType = question.chrType;
                if(chrType == '01' || chrType == '02'){
                    // html += _this.single_many_html(question, _this.question_paper * _this.load_length + i);
                    html += _this.single_many_html(question, question_div_num + i);
                    card_count += 1;
                }else if(chrType == '03'){
                    // html += _this.judge_html(question, _this.question_paper * _this.load_length + i);
                    html += _this.judge_html(question, question_div_num + i);
                    card_count += 1;

                    // 填空题
                }else if(chrType == '04'){
                    html += _this.fill_html(question, question_div_num + i);
                    card_count += 1;

                    // 简答 论述 计算  绘图
                } else if(chrType == '05' || chrType == '06' || chrType == '07' || chrType == '08') {
                    html += _this.subjective_html(question, question_div_num + i);
                    card_count += 1;
                }
            }

            $("#" + _this.body_id).append(html);
            _this.initCard(card_count);
            var last_number = _this.questionLength - questions.length -  _this.question_paper * _this.load_length;
            if(last_number > 0){
                $("#loadQuestion").find("span").html(last_number);
            }else{
                $("#loadQuestion").hide();
            }

            var scroll_height = $("#card_list").find("li:eq(0)").height() * (
                $("#card_list").find("li").length / 4 == 0 ?
                    $("#card_list").find("li").length / 4 :
                    $("#card_list").find("li").length / 4 + 1);

            $("#card_list").scrollTop(scroll_height + $("#card_list").scrollTop());
            _this.question_paper++;
            //判断是否加载完
            if(_this.question_paper > _this.questionIds.length - 1){
                $("#loadQuestion").hide();
                return;
            }
        }
        //图片懒加载
        $("img.lazy").lazyload({
            placeholder:'../../static/global/images/loading.gif',
            effect: "fadeIn",
            threshold : 2000
        });
    };

    _this.single_many_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions"  data-question="' + question.intId + '"  data-index="' + i + '">';
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（';
        if(chrType == '02'){
            html += '多选题';
        }else{
            html += '单选题';
        }
        html += '）</span><div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        for(var i=0; i<questionTitleFiles.length; i++){
            var file = questionTitleFiles[i];
            if(file.mimeType == 'img'){
                html += '<img class="lazy" data-original="' + file.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        var options = question.options;
        if(!options){
           return;
        }
        for(var i=0, length=options.length; i<length; i++){
            var q = options[i];
            html += '<div>';
            if(chrType == '02'){
                html += '<div class="option checkbox" data-type="' + q.oType + '">';
            }else{
                html += '<div class="option" data-type="' + q.oType + '">';
            }
            html += '<div><span>' + q.oType + '、</span><div>' + q.optionContent + '</div></div>';
            html += '</div>';
            html += '<div class="show_img clearfix">';
            if(q.mimeType == 'img'){
                html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ q.mimeUrl +'" alt="">';
            }
            html += '</div>';
            html += '</div>';
        }
        html += '</div>';

        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }
        html += '我的回答：' + myAnswer + '（';
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<span class="is_correct" style="color: #00b8c7">回答正确</span>';
        }else{
            html += '<span class="is_correct" style="color: red;">回答错误</span>';
        }
        html += '）</div>';
        html += '<div class="pull-right">';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<a href="javascript:;" class="analy">展开解析 <i class="fa fa-angle-down"></i></a>';
        }else{
            html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-up"></i></a>';
        }
        html += '</div>';
        html += '</div>';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<div class="subject_foot">';
        }else{
            html += '<div class="subject_foot" style="display: block;">';
        }
        html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';
        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles == undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(q.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ q.mimeUrl +'" alt="">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };

    _this.judge_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions"  data-question="' + question.intId + '" data-index="' + i + '">';
        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（判断题）</span><div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" src="'+ file.mimeUrl +'" alt="">';
                }
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';
        html += '<div>';
        html += '<div class="option" data-type="A">';
        html += '<div><span>A、</span><div>正确</div></div>';
        html += '</div>';
        html += '</div>';
        html += '<div>';
        html += '<div class="option" data-type="B">';
        html += '<div><span>B、</span><div>错误</div></div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';

        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }
        html += '我的回答：' + myAnswer + '（';
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<span class="is_correct" style="color: #00b8c7">回答正确</span>';
        }else{
            html += '<span class="is_correct" style="color: red;">回答错误</span>';
        }
        html += '）</div>';
        html += '<div class="pull-right">';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<a href="javascript:;" class="analy">展开解析 <i class="fa fa-angle-down"></i></a>';
        }else{
            html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-down"></i></a>';
        }
        html += '</div>';
        html += '</div>';
        if(isCorrect == 1 || isCorrect == '1'){
            html += '<div class="subject_foot">';
        }else{
            html += '<div class="subject_foot" style="display: block;">';
        }
        html += '<div><span class="label">正确答案：</span><i class="answer">' + aes_decrypt(question.varAnswer) + '</i></div>';
        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles == undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(q.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + q.mimeUrl + '" src="'+ q.mimeUrl +'" alt="">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };



    // 其他主观题
    _this.subjective_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '">';

        html += '<div class="subject"><div class="question_content clearfix">';

        // 题型名称
        var titleTypeName = "";
        if (chrType == '05') {
            titleTypeName = "（简答题）";
        } else if (chrType == '06') {
            titleTypeName = "（论述题）";
        } else if (chrType == '07') {
            titleTypeName = "（计算题）";
        } else if (chrType == '08') {
            titleTypeName = "（绘图题）";
        }
        var questionNum = (i+1);
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">' + titleTypeName + '</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';

        var answerStr = '';
        var imgStr = '';
        if(question.myAnswer != undefined && question.myAnswer!=''){
            if(question.myAnswer.split("&&").length > 1){
                answerStr = question.myAnswer.split("&&")[0];
                imgStr = question.myAnswer.split("&&")[1];
            }else{
                if(question.myAnswer.indexOf("play.do") != -1){
                    imgStr = question.myAnswer;
                }else{
                    answerStr = question.myAnswer;
                }
            }
        }

        html += '<div class="options">';

        /*html += '<div>';
        html += '<div>';
        html += '<span></span>';
        html += '<div><span>答案、</span><div style="position: relative;" class="answerImg"><textarea style="margin-right: 9px;" class="answer-input" disabled>'+answerStr+'</textarea>';
        if(imgStr != ''){
            html += '<img id="imgAnswer'+questionNum+'" magnify="0" src="'+imgStr+'" data-original="'+imgStr+'" style="width:67px;height: 67px;" onclick="magnifyImg('+questionNum+')"/><a onclick="imgRotate('+questionNum+')" rotateNum="-90" id="rotateImg'+questionNum+'" style="left:211px;margin-top:-3px;position: absolute;z-index: 2;color:#ea1c05;cursor: pointer;" title="旋转">☇</a>';
        }
        html += '</div></div>';
        html += '</div>';
        html += '</div>';*/

        html += '</div>';



        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        /*var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }*/
        html += '我的回答：' + answerStr ;
        if(imgStr != ''){
            html += '<img id="imgAnswer'+questionNum+'" magnify="0" src="'+imgStr+'" data-original="'+imgStr+'" style="width:67px;height: 67px;" onclick="magnifyImg('+questionNum+')"/><a onclick="imgRotate('+questionNum+')" rotateNum="-90" id="rotateImg'+questionNum+'" style="right:600px;margin-top:-3px;position: absolute;z-index: 2;color:#ea1c05;cursor: pointer;" title="旋转">旋转</a>';
        }
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '（<span class="is_correct" style="color: #00b8c7">回答正确</span>）';
        }else if(isCorrect == 2 || isCorrect == '2'){
            html += '（<span class="is_correct" style="color: red;">回答错误</span>）';
        }else{
            html += '<span class="is_correct" ></span>';
        }
        html += '</div>';

        /*html += '我的回答：' + answerStr ;
        if(imgStr != ''){
            html += '<img id="imgAnswer'+questionNum+'" magnify="0" src="'+imgStr+'" data-original="'+imgStr+'" style="width:67px;height: 67px;" onclick="magnifyImg('+questionNum+')"/><a onclick="imgRotate('+questionNum+')" rotateNum="-90" id="rotateImg'+questionNum+'" style="left:211px;margin-top:-3px;position: absolute;z-index: 2;color:#ea1c05;cursor: pointer;" title="旋转">☇</a>';
        }
        html += '</div>';*/
        html += '<div class="pull-right">';
        html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="subject_foot" style="display: block;">';

        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';

        // ***************************begin
        html += '<div class="show_img clearfix">';
        var varAnswerImages = question.varAnswerImages;
        if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
            var answerImagesArr = varAnswerImages.split(",");
            for (var j = 0; j <answerImagesArr.length ; j++) {
                html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
            }
        }
        html += '</div>';

        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;

            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl +'">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;

    };

    // 填空题
    _this.fill_html = function (question, i) {
        var chrType = question.chrType;
        var html = '';
        html += '';
        html += '<div id="div' + (i + 1) + '" class="questions" data-course="' + _this.course_id + '" data-question="' + question.intId + '" data-project="' + _this.project_id + '" data-index="' + i + '">';

        html += '<div class="subject"><div class="question_content clearfix">';
        html += '<span class="index">' + (i + 1) + '</span><span class="title_bg">（填空题）</span>' + '<div>' + question.varQuestionsContent + '</div></div>';
        html += '<div class="show_img clearfix">';
        var questionTitleFiles = question.questionTitleFiles;
        if(questionTitleFiles != undefined){
            for(var i=0; i<questionTitleFiles.length; i++){
                var file = questionTitleFiles[i];
                if(file.mimeType == 'img'){
                    html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="'+ file.mimeUrl + '">';
                }
            }
        }
        html += '</div>';
        html += '</div>';
        html += '<div class="options">';

        /*var titNum = checkTianKongNum(question.varQuestionsContent);

        var inputAnswer = '';
        var answers = [];
        if(question.myAnswer != undefined ){
            answers = question.myAnswer.split("|");
        }
        for (var j = 0; j < titNum; j++) {
            if(answers.length >= j && answers.length != 0){
                inputAnswer = answers[j];
            }
            html += '<div>';
            html += '<div>';
            html += '<span></span>';
            html += '<div><span>空 '+ (j+1) +' 答案、</span><div><input type="text" width="60px" class="answer-input" value="'+inputAnswer+'" disabled/></div></div>';
            html += '</div>';
            html += '</div>';
        }
         */
        html += '</div>';

        html += '<div class="option_select clearfix" data-type="' + question.chrType + '">';
        html += '<div class="pull-left">';
        var myAnswer = "";
        if(question.myAnswer){
            myAnswer = question.myAnswer;
        }
        html += '我的回答：' + myAnswer;
        var isCorrect = question.isCorrect;
        if(isCorrect == 1 || isCorrect == '1'){
            html += '（<span class="is_correct" style="color: #00b8c7">回答正确</span>）';
        }else if(isCorrect == 2 || isCorrect == '2'){
            html += '（<span class="is_correct" style="color: red;">回答错误</span>）';
        }else{
            html += '<span class="is_correct" ></span>';
        }
        html += '</div>';

        /*html += '我的回答：' + myAnswer;
        html += '</div>';*/
        html += '<div class="pull-right">';

        html += '<a href="javascript:;" class="analy">收起解析 <i class="fa fa-angle-down"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="subject_foot" style="display: block;">';

        html += '<div><span class="label">正确答案：</span><i class="answer">' + question.varAnswer + '</i></div>';
        html += '<div class="show_img clearfix">';
        var varAnswerImages = question.varAnswerImages;
        if(varAnswerImages != undefined && varAnswerImages != '' && varAnswerImages.length > 0){
            var answerImagesArr = varAnswerImages.split(",");
            for (var j = 0; j <answerImagesArr.length ; j++) {
                html += '<img class="lazy" data-original="' + answerImagesArr[j] + '" alt="" src="'+ answerImagesArr[j] +'">';
            }
        }
        html += '</div>';

        html += '<div class="clearfix">';
        html += '<span class="label">试题解析：</span>';
        html += '<div class="pull-left analysis">';
        var analysisContent = question.analysisContent;
        if(analysisContent == '' || analysisContent == undefined){
            html += '暂无解析';
        }else{
            html += question.analysisContent;
            html += '<div class="show_img clearfix">';
            var analysisFiles = question.analysisFiles;
            if(analysisFiles != undefined && analysisFiles.length > 0){
                for(var i=0; i<analysisFiles.length; i++){
                    var file = analysisFiles[i];
                    if(file.mimeType == 'img'){
                        html += '<img class="lazy" data-original="' + file.mimeUrl + '" alt="" src="' + file.mimeUrl + '">';
                    }
                }
            }
            html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        return html;
    };

// 检查题目内容括号个数
    function checkTianKongNum(title) {

        // 正则表达式
        var reg = /[\(|（]([^\(\)（）]*)[\)|）]/ig;

        var t = title.match(reg);

        if (t == null) {
            return 0;
        }

        // 总共括号个数
        var num = title.match(reg).length;

        // 返回括号个数
        return num;

    }

    _this.ajax = function(url, param, type) {
        var result;
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : param,
            success : function(data) {
                result = data;
            }
        });
        return result;
    };
}

var question_score_ajax = new question_score_ajax();